//
//  AdvertisentMainView.m
//  ProjectForCustomer
//
//  Created by 钟游 on 2019/8/25.
//  Copyright © 2019年 钟游. All rights reserved.
//

#import "AdvertisentMainView.h"
#import "LoginViewModel.h"
@interface AdvertisentMainView(){
    NSInteger count;
}
@property(nonatomic,strong)UIImageView * advertisementImageView;
@property(nonatomic,strong)UIImageView * logoImageView;
@property(nonatomic,strong)CustomButton * timeBtn;
@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,strong)LoginViewModel * viewModel;
@end
@implementation AdvertisentMainView
-(instancetype)initWithViewModel:(id<ViewModelProtocol>)viewModel{
    self.viewModel =(LoginViewModel*)viewModel;
    return  [super initWithViewModel:viewModel];
}
-(void)zy_bindViewModel{
    @weakify(self);
    [[self.viewModel.adSubject takeUntil:self.rac_willDeallocSignal]subscribeNext:^(id  _Nullable x) {
        @strongify(self);
       
        self->count = 3;
        [self.advertisementImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVICE_URL,x]] placeholderImage:[UIImage imageNamed:@"bg_third"]];
        self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }];
    [[self.viewModel.wbSubject takeUntil:self.rac_willDeallocSignal]subscribeNext:^(id  _Nullable x) {
        [self.viewModel.adCommand execute:nil];
    }];
}
-(void)zy_setupViews{
    [self addSubview:self.advertisementImageView];
    [self addSubview:self.logoImageView];
    [self addSubview:self.timeBtn];
  
}
#pragma mark -- jump
-(void)jump{
    [self.viewModel.weiSubject sendNext:nil];
}
#pragma mark -- 定时器
-(void)timerAction{
  
    if (count==0) {
        [self.timer invalidate];
        self.timer = nil;
        [self.timeBtn setTitle:@"跳过" forState:UIControlStateNormal];
    }else{
        [self.timeBtn setTitle:[NSString stringWithFormat:@"跳过%ld",count] forState:UIControlStateNormal];
        count--;
    }
    
}
#pragma mark -- lazy
-(LoginViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel=[[LoginViewModel alloc]init];
    }
    return _viewModel;
}
-(UIImageView *)advertisementImageView{
    if (!_advertisementImageView) {
        _advertisementImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KscreenWidth, KscreenHeight-72)];
    }
    return _advertisementImageView;
}
-(UIImageView *)logoImageView{
    if (!_logoImageView) {
        _logoImageView=[[UIImageView alloc]initWithFrame:CGRectMake(KscreenWidth/2-56, CGRectGetMaxY(self.advertisementImageView.frame)+20, 112,33)];
        _logoImageView.image=[UIImage imageNamed:@"启动页logo"];
    }
    return _logoImageView;
}
-(CustomButton *)timeBtn{
    if (!_timeBtn) {
        _timeBtn=[[CustomButton alloc]initWithFrame:CGRectMake(KscreenWidth-15-70, 32, 70, 38) andTitle:@"跳过 3" andColor:UIColorFromRGB(0XFFFFFF) andFont: 13];
        _timeBtn.backgroundColor=[UIColorFromRGB(0x111111) colorWithAlphaComponent:.7];
        _timeBtn.layer.cornerRadius = 15;
        _timeBtn.layer.masksToBounds = YES;
        [_timeBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    }
    return _timeBtn;
}
@end
